package com.higold.cloud.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.higold.cloud.R;
import com.higold.cloud.glide.GlideApp;
import com.higold.cloud.utils.ImageLoader;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.FullScreenPopupView;

import java.util.ArrayList;

import me.zhouzhuo.zzimagebox.ZzImageBox;

/**
 * author : lxz
 * date   : 2019/9/7 10:01
 * desc   :
 */
public class DevelopFullScreenPopup extends FullScreenPopupView {

    private DevelopFullScreenPopup.FullScreenPopupClickListener listener;
    private ArrayList<String> data;

    public DevelopFullScreenPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.sheet_add_shop;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        data = new ArrayList<>();

        EditText ed_shop_name = findViewById(R.id.ed_shop_name);
        EditText ed_contact_name = findViewById(R.id.ed_contact_name);
        EditText ed_contact_phone = findViewById(R.id.ed_contact_phone);
        EditText ed_address = findViewById(R.id.ed_address);
        EditText ed_address_detail = findViewById(R.id.ed_address_detail);
        EditText ed_business_license = findViewById(R.id.ed_business_license);

        ZzImageBox zb_shop_pic = this.findViewById(R.id.zb_shop_pic);
        ZzImageBox zb_license_pic = this.findViewById(R.id.zb_license_pic);
        setZzImageBox(zb_license_pic);
        setZzImageBox(zb_shop_pic);

        findViewById(R.id.tv_sure).setOnClickListener(v -> {
            data.add(ed_shop_name.getText().toString());
            data.add(ed_contact_name.getText().toString());
            data.add(ed_contact_phone.getText().toString());
            data.add(ed_address.getText().toString());
            data.add(ed_address_detail.getText().toString());
            data.add(ed_business_license.getText().toString());
            if (listener != null) {
                listener.okClick(data);
            }
        });

        findViewById(R.id.tv_cancel).setOnClickListener(v -> {
            if (listener != null) {
                listener.cancelClick();
            }
        });

    }

    private void setZzImageBox( ZzImageBox zzImageBox){
        //如果加载网络图片，需要设置此代理
        zzImageBox.setOnlineImageLoader((iv, url) -> {
            Log.e("TTT", "url=" + url);
            //使用Glide加载
            GlideApp.with(getContext())
                    .load(url)
                    .placeholder(R.color.color_darker_gray)
                    .transition(new DrawableTransitionOptions().crossFade())
                    .thumbnail(0.5f)
                    .into(iv);
        });

        zzImageBox.setOnImageClickListener(new ZzImageBox.OnImageClickListener() {
            @Override
            public void onImageClick(int position, String filePath, String tag, int type, ImageView iv) {
                new XPopup.Builder(getContext())
                        .asImageViewer(iv, filePath, true, -1, -1, 50, false, new ImageLoader())
                        .show();
            }

            @Override
            public void onDeleteClick(int position, String filePath, String tag, int type) {
                //移除position位置的图片
                zzImageBox.removeImage(position);
            }

            @Override
            public void onAddClick() {
                if (listener != null) {
                    listener.OnImageClick(zzImageBox);
                }
            }
        });
    }

    public interface FullScreenPopupClickListener {
        public void okClick(ArrayList<String> data);
        public void cancelClick();
        public void OnImageClick(ZzImageBox imageBoxAddMode);
    }

    public void setOnFullScreenPopupClickListener(DevelopFullScreenPopup.FullScreenPopupClickListener listener) {
        this.listener = listener;
    }

}
